package com.example.asus_pc.myshop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.asus_pc.myshop.R;
import com.example.asus_pc.myshop.adapter.EditVegaNumAdatpter;
import com.example.asus_pc.myshop.bean.VegaBean;
import com.example.asus_pc.myshop.utils.SharedPreUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AddBillListActivity extends AppCompatActivity implements View.OnClickListener{

    TextView tvChooseVega;
    TextView tvRight;
    private List<VegaBean> datas;

    private RecyclerView rvEditCount;
    private EditVegaNumAdatpter adatpter;
    private List<VegaBean> totalVegas;
    private HashMap<String,VegaBean> vegaMap=new HashMap<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bill_list);
        initViews();
        setListeners();

    }

    @Override
    protected void onResume() {
        super.onResume();
        String dataStr = SharedPreUtils.getString(SharedPreUtils.VEGA_CONTENT,"");
        datas =new ArrayList<>();
        totalVegas = VegaBean.jsonToList(dataStr);
        for (int i = 0; i < totalVegas.size(); i++) {
            VegaBean bean = totalVegas.get(i);
            if (totalVegas.get(i).getIsSelected()==1) {
                datas.add(bean);
            }
            vegaMap.put(bean.getVegaName(),bean);
        }
        adatpter.setDatas(datas);
    }

    private void setListeners() {

        findViewById(R.id.tv_add).setOnClickListener(this);
        findViewById(R.id.tv_right).setOnClickListener(this);

    }

    private void initViews() {


        rvEditCount = (RecyclerView) findViewById(R.id.rv_edit_count);
        rvEditCount.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        adatpter = new EditVegaNumAdatpter(this);
        rvEditCount.setAdapter(adatpter);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_add:
                startActivity(new Intent(AddBillListActivity.this,ChooseVegaActivity.class));
                break;
            case R.id.tv_right:
                List<VegaBean> datas = adatpter.getDatas();
                for (int i = 0; i < datas.size(); i++) {
                    VegaBean bean = datas.get(i);
                    vegaMap.put(bean.getVegaName(),bean);
                }
                List<VegaBean> total = new ArrayList<>();
                for (int i = 0; i < totalVegas.size(); i++) {
                    total.add(vegaMap.get(totalVegas.get(i).getVegaName()));
                }
                String dataStr = VegaBean.listToJson(total);
                SharedPreUtils.putString(SharedPreUtils.VEGA_CONTENT,dataStr);

                startActivity(new Intent(AddBillListActivity.this,ClipsActivity.class));
                break;
        }
    }
}
